package servlet;

import dao.WarehouseDAOImpl;
import pojo.Warehouse;
import utils.ApiResult;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/warehouse")
public class WarehouseServlet extends BaseServlet {

    // 添加仓库
    protected void addWarehouse(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String warehouseName = req.getParameter("warehouseName");
        String warehouseLocation = req.getParameter("warehouseLocation");
        int storageSpace = Integer.parseInt(req.getParameter("storageSpace"));

        Warehouse warehouse = new Warehouse( null,warehouseName, warehouseLocation, storageSpace);
        WarehouseDAOImpl.add(warehouse);
        sendJsonResponse(resp, new ApiResult(200, "Warehouse added successfully.", null));
    }

    // 删除仓库
    protected void deleteWarehouse(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        WarehouseDAOImpl.delete(warehouseNumber);
        sendJsonResponse(resp, new ApiResult(200, "Warehouse deleted successfully.", null));
    }

    // 更新仓库
    protected void updateWarehouse(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        String warehouseName = req.getParameter("warehouseName");
        String warehouseLocation = req.getParameter("warehouseLocation");
        int storageSpace = Integer.parseInt(req.getParameter("storageSpace"));

        Warehouse warehouse = new Warehouse(warehouseNumber, warehouseName, warehouseLocation, storageSpace);
        WarehouseDAOImpl.update(warehouse);
        sendJsonResponse(resp, new ApiResult(200, "Warehouse updated successfully.", null));
    }

    // 获取所有仓库
    protected void getAllWarehouses(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<Warehouse> warehouses = WarehouseDAOImpl.getAll();
        sendJsonResponse(resp, new ApiResult(200, "Success", warehouses));
    }

    // 根据ID获取仓库
    protected void getWarehouseById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int warehouseNumber = Integer.parseInt(req.getParameter("warehouseNumber"));
        Warehouse warehouse = WarehouseDAOImpl.getById(warehouseNumber);
        sendJsonResponse(resp, new ApiResult(200, "Success", warehouse));
    }

    // 获取仓库数量
    protected void getWarehouseCount(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int count = WarehouseDAOImpl.getCount();
        sendJsonResponse(resp, new ApiResult(200, "Success", count));
    }

    // 分页获取仓库
    protected void getWarehousesByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Warehouse> warehouses = WarehouseDAOImpl.getByPage(offset, pageSize);
        sendJsonResponse(resp, new ApiResult(200, "Success", warehouses));
    }

    // 根据关键字搜索仓库
    protected void findWarehousesByKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        List<Warehouse> warehouses = WarehouseDAOImpl.findByKeyword(keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", warehouses));
    }

    // 根据关键字分页搜索仓库
    protected void findWarehousesByPageAndKeyword(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String keyword = req.getParameter("keyword");
        int start = Integer.parseInt(req.getParameter("start"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        int offset = (start - 1) * pageSize;
        List<Warehouse> warehouses = WarehouseDAOImpl.findByPageAndKeyword(offset, pageSize, keyword);
        sendJsonResponse(resp, new ApiResult(200, "Success", warehouses));
    }
}
